#include"head.h"
//ret = -1 , -2 ,1
//-1 mysql select faild
//-2 mysql insert faild
//1 mysql insert success

//did not need send to cilent,just update current_path
//did not test 
int mkdir_m(MYSQL *db , char current_path , char *filename)
{
    MYSQL *db_cd = db ;

    // ret_val_t ret_info ;
    // ret_info.check_value = 0 ;
    // memset(ret_info.path_info,0,sizeof(ret_info.path_info)) ;
    
    int ret ;
    const char slash[] = "/" ;
    const char apostrophe_semicolon[] = "' ;"  ;
    const char tail[] = " );"  ;

    char buf[1024] = "SELECT * FROM FileTable WHERE path = '" ;
    strcat(buf,current_path) ;
    strcat(buf,apostrophe_semicolon) ;

    char *sql = buf ;
    int qret = mysql_query(db_cd,sql) ;

    if(qret != 0)
    {
        //ret_info.check_value = -1 ;//-1 查询数据库失败
        //strcpy(ret_info.check_value , mysql_error(db_cd)) ;
        ret = -1 ;//查询数据库失败

        return ret ;
    }

    MYSQL_RES *result = mysql_store_result(db_cd) ;
    MYSQL_ROW row ;
    row = mysql_fetch_row(result) ;

    char *username = row[2] ;
    char *current_path[100];
    bzero(current_path,sizeof(current_path));

    int pre_id = atoi(row[0])  ;
    char type[] = "dir" ;
    char *path[100] = {0} ;
    //char MD5[33] = "NULL" ;
    int tomb = 0 ;

    // strcat(path,current_path) ;
    // strcat(path,slash) ;
    // strcat(path,filename) ;

    //使用动态拼接
    bzero(buf,sizeof(bzero)) ;
    sprintf(buf,"INSERT INTO FileTable (filename,username,preid,type,path,tomb) VALUES \
    ('%s','%s','%d','%s','%s','%d')", filename,username,pre_id,type,path,tomb) ;
    db_cd = db ;

    qret = mysql_query(db_cd,buf) ;
    if(qret != 0)
    {
        ret = -2 ;//插入数据库失败

        return ret ;
    }

    else
    {
        return 1 ;//插入成功
    }




    //strcat(buf,current_path) ;
    //buf[1024] = "INSERT INTO FileTable (filename,username,preid,type,path,tomb) VALUES \
    (" ;
    // ret_md5_t md5_ret = create_md5(filename,username,path) ;
    // if(md5_ret.check_value == 1 )
    // {
    //     strcpy(MD5,md5_ret.md5_val) ;
    // }
    // else
    // {
    //     return -2 ;//md5 计算问题
    // }

}

